function [ data tep tes tea trp trs tra vap vas vaa ] = getSets(data)
load_settings;
% Caveat: assumes 1D array, so call this on data ONLY from one camera
%disp(['Getting sets for camera: ' data(1).cam_id]);
annotation = struct('image', '', 'rect', []);
tep = [];
tes = [];
tei = 1;
tea = annotation;
trp = [];
trs = [];
tri = 1;
tra = annotation;
vap = [];
vas = [];
vai = 1;
vaa = annotation;

% hold all rectanges
terect = [];
trrect = [];
varect = [];

% Init each set with one image to make sure they're not empty
if data(1).cam_id > 0
    data(1).set = 'test';
    tep = [ones(size(data(1).ppl_hogs, 1),1); -ones(size(data(1).bg_hogs,1),1)];
    tes = [data(1).ppl_hogs; data(1).bg_hogs];
	terect = [data(1).ppl_rects; data(1).bg_rects];
    for ix = 1:length(tep)
        tea(tei).image = [ image_root sprintf('%08d', data(1).cam_id) '/' data(1).filename];
        tea(tei).rect = terect(tei, :);
        tei = tei+1;
    end
end
if data(2).cam_id > 0
    data(2).set = 'train';
    trp = [ones(size(data(2).ppl_hogs, 1),1); -ones(size(data(2).bg_hogs,1),1)];
    trs = [data(2).ppl_hogs; data(2).bg_hogs];
	trrect = [data(2).ppl_rects; data(2).bg_rects];
    for ix = 1:length(trp)
        tra(tri).image = [ image_root sprintf('%08d', data(2).cam_id) '/' data(2).filename];
        tra(tri).rect = trrect(tri, :);
        tri = tri+1;
    end
end
if data(3).cam_id > 0
    data(3).set = 'valid';
    vap = [ones(size(data(3).ppl_hogs, 1),1); -ones(size(data(3).bg_hogs,1),1)];
    vas = [data(3).ppl_hogs; data(3).bg_hogs];
	varect = [data(3).ppl_rects; data(3).bg_rects];
    for ix = 1:length(vap)
        vaa(vai).image = [ image_root sprintf('%08d', data(3).cam_id) '/' data(3).filename];
        vaa(vai).rect = varect(vai, :);
        vai = vai+1;
    end
end

% And now for the rest of the images...
rng(3,'twister');	% set a random seed so data doesn't change
for ix = 4:length(data)
    r = rand;
    if r < 1/3
        if data(ix).cam_id > 0
            data(ix).set = 'test';
            tep = [tep ; ones(size(data(ix).ppl_hogs, 1),1); -ones(size(data(ix).bg_hogs,1),1)];
            tes = [tes ; data(ix).ppl_hogs; data(ix).bg_hogs];
			terect = [terect ; data(ix).ppl_rects; data(ix).bg_rects];
            start = tei;
            for jx = start:length(tep)
                tea(tei).image = [ image_root sprintf('%08d', data(ix).cam_id) '/' data(ix).filename];
                tea(tei).rect = terect(tei, :);
                tei = tei+1;
            end
        end
    elseif r < 2/3
        if data(ix).cam_id > 0
            data(ix).set = 'train';
            trp = [trp ; ones(size(data(ix).ppl_hogs, 1),1); -ones(size(data(ix).bg_hogs,1),1)];
            trs = [trs ; data(ix).ppl_hogs; data(ix).bg_hogs];
			trrect = [trrect ; data(ix).ppl_rects; data(ix).bg_rects];
            start = tri;
            for jx = start:length(trp)
                tra(tri).image = [ image_root sprintf('%08d', data(ix).cam_id) '/' data(ix).filename];
                tra(tri).rect = trrect(tri, :);
                tri = tri+1;
            end
        end
    else
        if data(ix).cam_id > 0
            data(ix).set = 'valid';
            vap = [vap; ones(size(data(ix).ppl_hogs, 1),1); -ones(size(data(ix).bg_hogs,1),1)];
            vas = [vas; data(ix).ppl_hogs; data(ix).bg_hogs];
			varect = [varect ; data(ix).ppl_rects; data(ix).bg_rects];
            start = vai;
            for jx = start:length(vap)
                vaa(vai).image = [ image_root sprintf('%08d', data(ix).cam_id) '/' data(ix).filename];
                vaa(vai).rect = varect(vai, :);
                vai = vai+1;
            end
        end
    end
end
%disp('Done getting sets');
end